/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.api.webcam.images;

import java.awt.*;
import java.awt.image.*;

public class RectangleGrabber {

  // grabs pixels in a rectangle
  // can also be used to store them

  // width and height of capture region
  int w, h;
  int[] pixels;

  public RectangleGrabber(int w, int h) {
    this.w = w;
    this.h = h;
    pixels = new int[w * h];
  }

  public RectangleGrabber(int w, int h, int[] pixels) {
    this.w = w;
    this.h = h;
    this.pixels = pixels;
  }

  public int[] grab(Image img, int x, int y) {
    return getPixels(img, x, y, w, h, pixels);
  }

  public static byte pix(int p) {
    return (byte) (p & 0xff);
  }

  public static byte[][] getBytes(Image img, int x, int y, int w, int h) {
    int[] pixels = getPixels(img, x, y, w, h);
    return getBytes(pixels, w, h);
  }

  public static byte[][] getBytes(int[] pixels, int w, int h) {
    if (pixels != null) {
      byte[][] ba = new byte[w][h];
      for (int i=0; i<w; i++)
        for (int j=0; j<h; j++) {

          ba[i][j] = pix(pixels[w * j + i]);

        }
      return ba;
    }
    else
      return null;
  }

  public static byte[][] getBytes(int[] pixels, byte[][] ba) {
    if ((pixels != null) && (ba != null) ) {
      int w = ba.length;
      int h = ba[0].length;
      for (int i=0; i<w; i++)
        for (int j=0; j<h; j++) {

          ba[i][j] = pix(pixels[w * j + i]);

        }
      return ba;
    }
    else
      return null;
  }

  public static byte[][] getBytes(byte[][] source, byte[][] dest, int x, int y) {
    int w = dest.length;
    int h = dest[0].length;
    for (int i=0; i<w; i++)
      for (int j=0; j<h; j++) {
        dest[i][j] = source[x + i][y + j];
      }
    return dest;
  }

  public static int[] getPixels(Image img, int x, int y, int w, int h) {
    int[] pixels = new int[w * h];
    return getPixels(img, x, y, w, h, pixels);
  }

  public static int[] getPixels(Image img, int x, int y, int w, int h, int[] pixels) {
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    try {
      pg.grabPixels();
    }
    catch (InterruptedException e) {
      System.err.println("interrupted waiting for pixels!");
      return null;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
      System.err.println("image fetch aborted or errored");
      return null;
    }
    return pixels;
  }
}

